import nx
from random import randint


def main():
    pass


if __name__ == '__main__':
    main()


class Character:
    def __init__(self):
        self.name = ""
        self.health = 1
        self.health_max = 1

    def do_damage(self, enemy):
        damage = min(
            max(randint(0, self.health) - randint(0, enemy.health), 0),
            enemy.health)
        enemy.health = enemy.health - damage
        if damage == 0:
            print("%s evades the %s's attack." %
                  (enemy.name, self.name), flush=True)
        else:
            print("%s hurts %s!" % (self.name, enemy.name), flush=True)
        return enemy.health <= 0


class Enemy(Character):
    def __init__(self, player):
        Character.__init__(self)
        self.name = 'goblin'
        self.health = randint(1, player.health)


class Player(Character):
    def __init__(self):
        Character.__init__(self)
        self.state = 'normal'
        self.health = 10
        self.health_max = 10

    def quit(self):
        print(
            "%s can't find the way back home, and dies of starvation.\nR.I.P." % self.name, flush=True)
        self.health = 0

    def help(self): print(list(Commands.keys()), flush=True)
    def status(self): print("%s's health: %d/%d" %
                            (self.name, self.health, self.health_max), flush=True)

    def tired(self):
        print("%s feels tired." % self.name, flush=True)
        self.health = max(1, self.health - 1)

    def rest(self):
        if self.state != 'normal':
            print("%s can't rest now!" % self.name, flush=True)
            self.enemy_attacks()
        else:
            print("%s rests." % self.name, flush=True)
            if randint(0, 1):
                self.enemy = Enemy(self)
                print("%s is rudely awakened by a %s!" %
                      (self.name, self.enemy.name), flush=True)
                self.state = 'fight'
                self.enemy_attacks()
            else:
                if self.health < self.health_max:
                    self.health = self.health + 1
                else:
                    print("%s slept too much." % self.name, flush=True)
                    self.health = self.health - 1

    def explore(self):
        if self.state != 'normal':
            print("%s is too busy right now!" % self.name, flush=True)
            self.enemy_attacks()
        else:
            print("%s explores a twisty passage." % self.name, flush=True)
            if randint(0, 1):
                self.enemy = Enemy(self)
                print("%s encounters a %s!" %
                      (self.name, self.enemy.name), flush=True)
                self.state = 'fight'
            else:
                if randint(0, 1):
                    self.tired()

    def flee(self):
        if self.state != 'fight':
            print("%s runs in circles for a while." % self.name, flush=True)
            self.tired()
        else:
            if randint(1, self.health + 5) > randint(1, self.enemy.health):
                print("%s flees from the %s." %
                      (self.name, self.enemy.name), flush=True)
                self.enemy = None
                self.state = 'normal'
            else:
                print("%s couldn't escape from the %s!" %
                      (self.name, self.enemy.name), flush=True)
                self.enemy_attacks()

    def attack(self):
        if self.state != 'fight':
            print("%s swats the air, without notable results." %
                  self.name, flush=True)
            self.tired()
        else:
            if self.do_damage(self.enemy):
                print("%s executes the %s!" %
                      (self.name, self.enemy.name), flush=True)
                self.enemy = None
                self.state = 'normal'
                if randint(0, self.health) < 10:
                    self.health = self.health + 1
                    self.health_max = self.health_max + 1
                    print("%s feels stronger!" % self.name, flush=True)
            else:
                self.enemy_attacks()

    def enemy_attacks(self):
        if self.enemy.do_damage(self):
            print("%s was slaughtered by the %s!!!\nR.I.P." %
                  (self.name, self.enemy.name), flush=True)


Commands = {
    nx.p1.controller.zr_button: Player.quit,  # ZR Button
    nx.p1.controller.plus_button: Player.help,  # + Button
    nx.p1.controller.r_button: Player.status,  # R Button
    nx.p1.controller.x_button: Player.rest,  # X Button
    nx.p1.controller.y_button: Player.explore,  # Y Button
    nx.p1.controller.b_button: Player.flee,  # B Button
    nx.p1.controller.a_button: Player.attack,  # A Button
}

p = Player()
p.name = input("What is your character's name? ")
print("(press +  to get a list of actions)\n", flush=True)
print("%s enters a dark cave, searching for adventure." % p.name, flush=True)

while(p.health > 0):
    print("Awaiting Input")
    # Detect Button Input
    buttonInput = any_pressed(player='p1', refresh_input=True)
    args = buttonInput.split()
    if len(args) > 0:
        commandFound = False
        for c in list(Commands.keys()):
            if args[0] == c[:len(args[0])]:
                Commands[c](p)
                commandFound = True
                break
        if not commandFound:
            print("%s doesn't understand the suggestion." % p.name, flush=True)
